package com.bawei.service.impl;

import com.bawei.mapper.AttendanceMapper;
import com.bawei.pojo.Attendance;
import com.bawei.service.AttendanceService;
import com.bawei.util.PageView;
import com.bawei.util.ResultEntity;
import com.bawei.util.StringUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;


@Service
@Transactional
public class AttendanceServiceImpl implements AttendanceService {

    @Autowired
    private AttendanceMapper attendanceMapper;


    @Override
    public PageView attendanceList(String currentPage, String signInStr, String signOutStr, String uid) {
        PageHelper.startPage(Integer.parseInt(currentPage), 3);
        List<Attendance> list = attendanceMapper.attendanceList(signInStr, signOutStr, uid);
        PageInfo<Attendance> attendances = new PageInfo<Attendance>(list);
        PageView<Attendance> pageView = new PageView<>();
        pageView.setTotalPage(attendances.getTotal());//总页数
        pageView.setRecords(list);
        pageView.setCurrentPage(Integer.parseInt(currentPage));
        pageView.setTotalRecord(Long.valueOf(attendances.getTotal()));//总条数
        return  pageView;
    }

    @Override
    public Attendance selectSignInStr(String id){
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String newDate = format.format(date);
        return attendanceMapper.selectSignInStr(id,newDate);
    }

    @Override
    public ResultEntity insertSignIn(String id, HttpServletRequest request) {
        Attendance attendance = new Attendance();
        attendance.setUserId(id);
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        request.getSession().setAttribute("uuid", uuid);
        attendance.setId(uuid);
        Date date=this.check();
        attendance.setSignIn(date);
        attendance.setIp(StringUtil.getIpAddr(request));
        int count = attendanceMapper.insert(attendance);
        if(count>0){
            request.setAttribute("isSignIn", "1");
            return new ResultEntity("200", "签到成功");
        }
        return new ResultEntity("400", "服务器错误");
    }

    @Override
    public ResultEntity updateSignOut(String id ) {
        Attendance attendance = new Attendance();
        attendance.setSignOut(this.check());
        attendance.setId(id);
        int count = attendanceMapper.updateByPrimaryKeySelective(attendance);
        if(count>0){
           /* request.setAttribute("isSignIn", "1");*/
            return new ResultEntity("200", "签退成功");
        }
        return new ResultEntity("400", "服务器错误");
    }

    private Date check() {
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String newDate = format.format(date);
        try {
            return  format.parse(newDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }
}
